import store from "../store";
import { baseURL } from '@/constant/system.js'
import saveAs from "file-saver";
// 全局方法
// 非空判断
export function isNotBlank(val) {
  return (val && val != undefined && val != null && val != "");
}
// 判空
export function isBlank(val) {
  return (val == undefined || val == null || val == "");
}
// 数组非空判断
function arrIsNotEmpty(arrVal) {
  return arrVal != undefined && arrVal.length > 0;
}

/**
 * 是否有权限, 用于下拉框的操作权限(类似v-permission),返回true/false, 结合v-if使用
 * @param {*} val 权限数组
 * @returns  返回true/false
 */
function hasPermission(val) {
  let muneList = store.state.menuList;
  let flag = val.some((permissionCode) => {
    return muneList.some((item) => {
      return item.rightsCode = permissionCode;
    });
  });
  return flag;
}
// 清空表单数据(彻底解决新增修改公用一个页面的this.$refs.formRef.resetFields()失效问题)
function clearFormData(val) {
  for (let key in val) {
    if (key !== "pageNo" && key !== "pageSize") {
      val[key] = undefined;
    }
  }
}
/**
 * 重置表单数据
 * @param {*} orgObj  原表单对象
 * @param {*} destObj 目标值对象
 */
function resetFormData(orgObj, destObj) {
  for (let key in orgObj) {
    if (destObj) {
      orgObj[key] = destObj[key];
    } else {
      orgObj[key] = undefined;
    }
  }
}
// 获取外部的静态资源路径
function getOutResource(val) {
  let path = baseURL + val;
  return path;
}
// 获取外部的静态资源路径
function getKey(val) {
  return new Date().getTime();
}
// 根据后端返回的菜权限单, 修改前端的静态路由
function getAllRouteMenuList(routes, menuList) {
  let allRoute = getAllRoutes(routes, menuList);
  let realRoutes = buildRoute(allRoute, [{rightsId:"0"}]);
  return realRoutes;
}
/**
 * 构建路由
 * @param {*} routes 前端静态路由
 * @param {*} parentRouteList  父级菜单
 * @returns 返回构建好的路由(带层级关系)
 */
function buildRoute(routes, parentRouteList) {
  let returnList = [];
  if (arrIsNotEmpty(parentRouteList)) {
    parentRouteList.forEach((parentMenu) => {
      // 父路由
      let parentRoute = routes.find((item) => item.rightsId == parentMenu.rightsId);
      // 子路由
      let routeList = routes.filter((item) => item.parentRightsId == parentMenu.rightsId);
      // 排序
      routeList.sort((a, b) => {
        return b.sort - a.sort;
      });
      if (parentRoute && parentMenu.rightsId != "0") {
        parentRoute.children = routeList;
        returnList.push(parentRoute);
      } else {
        returnList = returnList.concat(routeList);
      }
      if (arrIsNotEmpty(routeList)) {
        routeList.sort((a,b)=> a.sort - b.sort)
        buildRoute(routes,routeList)
      }
    });
  }
  returnList.sort((a,b)=> a.sort - b.sort)
  return returnList;
}
// 将所有的静态路由, 全部解析成一级路由, 并更具后端返回的权限菜单, 带上每个路由的父级标识
function getAllRoutes(routes, menuList) {
  let returnList = [];
  routes.forEach((route) => {
    // 处理父级菜单, 以后端返回的权限菜单为准
    if (route.path) {
      let menu = menuList.find((item) => item.path == route.path);
      if (menu) {
        returnList.push({
          routeName: route.name,
          name: menu.rightsName,
          path: route.path,
          icon: route.icon,
          sort: menu.sort || route.sort,
          rightsId: menu.rightsId,
          parentRightsId: menu.parentRightsId,
          children: [],
        });
      }
    }
    // 处理子级菜单
    if (route.children && route.children.length > 0) {
      let sonList = getAllRoutes(route.children, menuList);
      returnList = returnList.concat(sonList);
    }
  });
  return returnList;
}
function commonDownload(res) {
  const blob = new Blob([res.data], { type: res.headers['content-type'] });
  // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
  const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
  const contentDisposition = decodeURI(res.headers['content-disposition']);
  const result = patt.exec(contentDisposition);
  let fileName = result ? result[1] : 'file';
  fileName = fileName.replace(/"/g, '');
  console.log('文件名', fileName);
  saveAs(blob, fileName);
}

export default function globalMethod(app) {
  // 绑定全局函数
  app.config.globalProperties.arrIsNotEmpty = arrIsNotEmpty;
  app.config.globalProperties.isNotBlank = isNotBlank;
  app.config.globalProperties.isBlank = isBlank;
  app.config.globalProperties.hasPermission = hasPermission;
  app.config.globalProperties.clearFormData = clearFormData;
  app.config.globalProperties.resetFormData = resetFormData;
  app.config.globalProperties.getOutResource = getOutResource;
  app.config.globalProperties.getKey = getKey;
  app.config.globalProperties.getAllRouteMenuList = getAllRouteMenuList;
  app.config.globalProperties.commonDownload = commonDownload;
}
